//
//  UserCarSelectPerson.m
//  xjairOA
//
//  Created by apple on 16-1-27.
//  Copyright (c) 2016年 net. All rights reserved.
//

#import "UserCarSelectPerson.h"
#import "Constants.h"

@implementation UserCarSelectPerson
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //NSLog(@"dept is %@",self.deptId);
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
    self.appDelegate = [UIApplication sharedApplication].delegate;
    NSDictionary * params = @{@"model":@"loaddeptusers",@"deptId":self.deptId};
    //self.dataArray = [NSArray arrayWithObjects:@"张三",@"李四",@"王五",@"甲六",nil];
    self.dataArray = [[NSMutableArray alloc] init];
    [self.appDelegate.manager
     GET:UserCarMutiAction
     parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         self.data = responseObject;
         for (NSDictionary * dict in self.data){
             //NSLog(@"%@",[dict objectForKey:@"userName"]);
             [self.dataArray addObject:[dict objectForKey:@"userName"]];
         }
         [self.tableview reloadData];
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         UIAlertView * alert = [[UIAlertView alloc]
                                initWithTitle:@"提示"
                                message:@"获取服务器响应出错！"
                                delegate:nil
                                cancelButtonTitle:@"确定"
                                otherButtonTitles:nil];
         [alert show];
     }];

    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonSystemItemCancel target:self action:@selector(finish)];
    [self.navigationItem setRightBarButtonItem:rightButton];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"usecarSelectPersonCell" forIndexPath:indexPath];
    cell.layer.cornerRadius=12;
    cell.layer.masksToBounds=YES;
    UILabel * label = (UILabel*)[cell viewWithTag:1];
    label.text = [self.dataArray objectAtIndex:indexPath.row];
    return cell;
    
}

- (void)finish
{
    UILabel * label2 = (UILabel*)[((UIViewController*)[self.navigationController.viewControllers objectAtIndex:2]).view viewWithTag:2];
    NSMutableString * string = [[NSMutableString alloc] init];
    NSArray * indexPaths = [self.tableview indexPathsForSelectedRows];
    for(NSIndexPath *indexPath in indexPaths){
        UITableViewCell* cell = [self.tableview cellForRowAtIndexPath:indexPath];
        UILabel * label = (UILabel*)[cell viewWithTag:1];
        //NSLog(@"lable.text=%@",label.text);
        [string appendString:label.text];
        [string appendString:@" "];
    }
    label2.text=string;
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
}

@end
